﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Web;

namespace Project.Models
{

    public class FileUpload
    {

        
        string _UserName = "";         //User Name of the FTP server
        string _Password = "";      //Password of the FTP server
        string _ftpDirectory = "";  //The directory in FTP server where the file will be checked
        
        string _ftpServerIP = "";
        public FileUpload()
        {
            
            _UserName = "sunset";         //User Name of the FTP server
            _Password = "fu5aFEqa";      //Password of the FTP server
            _ftpDirectory = @"/UploadFiles/";  //The directory in FTP server where the file will be checked
                //File name, which one will be checked

            _ftpServerIP = "216.3.4.67";
          

            
        }
        public List<string>  ListFiles()
        {
            try
            {
                FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://" + _ftpServerIP + _ftpDirectory);
                request.Method = WebRequestMethods.Ftp.ListDirectory;

                request.UseBinary = false;
                request.UsePassive = false;
                request.Credentials = new NetworkCredential(_UserName, _Password);
                FtpWebResponse response = (FtpWebResponse)request.GetResponse();
                System.IO.Stream responseStream = response.GetResponseStream();
                StreamReader reader = new StreamReader(responseStream);
                string names = reader.ReadToEnd();

                reader.Close();
                response.Close();

                return names.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries).ToList();
            }
            catch (Exception)
            {
                throw;
            }
        }

        public bool CheckIfFileExistsOnServer(string fileName)
        {
            try
            {
                var request = (FtpWebRequest)WebRequest.Create("ftp://" + _ftpServerIP + _ftpDirectory + fileName);
                request.Credentials = new NetworkCredential(_UserName, _Password);
                request.Method = WebRequestMethods.Ftp.GetFileSize;
                request.KeepAlive = false;
                List<string> listnames = ListFiles();
                bool b = listnames.Any(fileName.Contains);
                return b;
            }
            catch (Exception)
            {
                return false;
                
            }
            

               


            try
            {
                //FtpWebResponse response = (FtpWebResponse)request.GetResponse();
                //return true;
            }
            catch (WebException ex)
            {
                FtpWebResponse response = (FtpWebResponse)ex.Response;
                if (response.StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable)
                    return false;
            }
            return false;
        }

        //public bool CheckFileExistOrNot(string FileName)
        //{

        //    FtpWebRequest ftpRequest = null;
        //    FtpWebResponse ftpResponse = null;
        //    bool IsExists = true;
        //    try
        //    {
        //        ftpRequest = (FtpWebRequest)WebRequest.Create("ftp://" + _ftpServerIP + _ftpDirectory + FileName);
        //        ftpRequest.Credentials = new NetworkCredential(_UserName, _Password);
        //        ftpRequest.Method = WebRequestMethods.Ftp.GetFileSize;
        //        ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
        //        ftpResponse.Close();
        //        ftpRequest = null;
        //    }
        //    catch (Exception ex)
        //    {
        //        IsExists = false;
        //    }
        //    return IsExists;

        public void DeleteFile(string FileName)
        {
            bool isfileexists = CheckIfFileExistsOnServer(FileName);

            if (isfileexists == true)
            {
                FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create("ftp://" + _ftpServerIP + _ftpDirectory + FileName);
                ftpRequest.Credentials = new NetworkCredential(_UserName, _Password);
                ftpRequest.Method = WebRequestMethods.Ftp.DeleteFile;
                FtpWebResponse responseFileDelete = (FtpWebResponse)ftpRequest.GetResponse();
            }
           
            
        }

        //public bool DeleteFile1(string FileName)
        //{
        //    bool IsDelete = true;
        //    try
        //    {
        //        if (CheckFileExistOrNot(FileName))
        //        {
        //            FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create("ftp://" + _ftpServerIP + _ftpDirectory + "/" + FileName);
        //            ftpRequest.Credentials = new NetworkCredential(_UserName, _Password);
        //            ftpRequest.Method = WebRequestMethods.Ftp.DeleteFile;
        //            FtpWebResponse responseFileDelete = (FtpWebResponse)ftpRequest.GetResponse();
        //        }
        //        else
        //        {
        //            IsDelete = false;
        //        }
        //    }
        //    catch (Exception)
        //    {

        //        IsDelete=false;
        //    }
        //    return IsDelete;
           
        //}


     
    }




}